SFTP Client PRO

Requires Scripting PRO

SFTPClient provides access to a remote file system over an SSH connection using the SFTP protocol. It supports directory operations, file management, attribute retrieval, and path resolution. Files can be opened using openFile(), which returns an SFTPFile instance for reading and writing.

Instances of this class are typically created through:

1const sftp = await ssh.openSFTP()

Properties

readonly isActive: boolean

Indicates whether the SFTP connection is still active.

  • true: The connection is active
  • false: The connection is closed or invalid

Methods


close(): Promise<void>

Closes the SFTP connection.

Returns:

  • A promise that resolves when the connection is successfully closed.

Example:

1await sftp.close()

readDirectory(atPath: string): Promise<DirectoryEntry[]>

Reads the contents of a directory.

Parameters:

  • atPath: The path of the directory to read.

Returns:

An array of directory entries:

1{
2  filename: string
3  longname: string
4  attributes: {
5    size?: number
6    userId?: number
7    groupId?: number
8    accessTime?: Date
9    modificationTime?: Date
10    permissions?: number
11  }
12}[]

Example:

1const items = await sftp.readDirectory("/var/log")

createDirectory(atPath: string): Promise<void>

Creates a directory at the specified path.

Parameters:

  • atPath: The path where the directory should be created.

Returns:

A promise that resolves when the directory is created.

Example:

1await sftp.createDirectory("/home/user/new-folder")

removeDirectory(atPath: string): Promise<void>

Removes a directory. The directory must be empty.

Parameters:

  • atPath: The directory path to remove.

Example:

1await sftp.removeDirectory("/home/user/empty-dir")

rename(oldPath: string, newPath: string): Promise<void>

Renames or moves a file or directory.

Parameters:

  • oldPath: The current path.
  • newPath: The new path.

Example:

1await sftp.rename("/home/user/a.txt", "/home/user/b.txt")

getAttributes(atPath: string): Promise<FileAttributes>

Retrieves file or directory metadata.

Returns:

1{
2  size?: number
3  userId?: number
4  groupId?: number
5  accessTime?: Date
6  modificationTime?: Date
7  permissions?: number
8}

Example:

1const attrs = await sftp.getAttributes("/etc/hosts")

openFile(filePath: string, flags: SFTPOpenFileFlags | SFTPOpenFileFlags[]): Promise<SFTPFile>

Opens a file with the specified flags and returns an SFTPFile instance.

Parameters:

  • filePath: The path of the file to open.
  • flags: One or more of the following:
"read" | "write" | "append" | "create" | "truncate" | "forceCreate"

Returns:

  • A promise that resolves to an SFTPFile object.

Example:

1const file = await sftp.openFile("/home/user/log.txt", ["read"])
2const data = await file.readAll()
3await file.close()

remove(atPath: string): Promise<void>

Removes a file.

Parameters:

  • atPath: The file path to remove.

Example:

1await sftp.remove("/home/user/old.txt")

getRealPath(atPath: string): Promise<string>

Resolves symbolic links, ~, and relative paths to an absolute path.

Example:

1const real = await sftp.getRealPath("~/documents")

Usage Example

1const ssh = await SSHClient.connect({
2  host: "192.168.1.10",
3  authenticationMethod: SSHAuthenticationMethod.passwordBased("user", "pass")
4})
5
6const sftp = await ssh.openSFTP()
7
8// Read a directory
9const list = await sftp.readDirectory("/home/user")
10
11// Open a file and read contents
12const file = await sftp.openFile("/home/user/info.txt", "read")
13const data = await file.readAll()
14await file.close()
15
16// Create a directory
17await sftp.createDirectory("/home/user/new-folder")
18
19// Delete a file
20await sftp.remove("/home/user/temp.txt")
21
22await sftp.close()